<?php
	include("includes/connect_db.php");

	$sql = $_POST['sql'];
	$sql = strtolower($sql);
	$host = $_POST['host'];
	$keyword = $_POST['keyword'];
	$tabel = $_POST['tabel'];
	$output = $_POST['output'];
	$parameter = $_POST['parameter'];
	$fungsi = $_POST['fungsi'];
	$fungsiParameter = $_POST['fungsiParameter'];
	$prosedur = $_POST['prosedur'];
	$prosedurParameter = $_POST['prosedurParameter'];
	$karakter = $_POST['karakter'];

	$i = 1;
    $rincianTabel = explode(',', $tabel);
    $jumlahTabel = count($rincianTabel);
    foreach ($rincianTabel as $parseTabel) {
        $parseTabel = ltrim($parseTabel);
        $arrayTabel[$i] = $parseTabel;
        $i++;
    }

    // PARSING OUTPUT
    $x = 1;
    $y = 1;
    $rincianOutput = explode(',', $output);
    $jumlahOutput = count($rincianOutput);
    if ($jumlahOutput > 1) {
	    foreach ($rincianOutput as $parseOutput) {
	    	$parseOutput = ltrim($parseOutput);
	    	if ($y > 3) {
	    		$y = 1;	
	        	$x++;
	        	// echo "<br/>";
	    	}
	        $arrayOutput[$x][$y] = $parseOutput;
	        // echo $arrayOutput[$x][$y];
	        $y++;
	    }
	    $jumlahOutput = $x;
    } else{
    	$jumlahOutput = 0;
    }
    // echo $jumlahOutput."<br/>";

    // PARSING PARAMETER
	$x = 1;
    $y = 1;
    $rincianParameter = explode(',', $parameter);
    $jumlahParameter = count($rincianParameter);
    if ($jumlahParameter > 1) {
	    foreach ($rincianParameter as $parseParameter) {
	    	if ($y > 3) {
	    		$y = 1;	
	        	$x++;
	        	// echo "<br/>";
	    	}
	    	$parseParameter = ltrim($parseParameter);
	        $arrayParameter[$x][$y] = $parseParameter;
	        // echo $arrayParameter[$x][$y];
	        $y++;
	    }
	    $jumlahParameter = $x;
	} else{
		$jumlahParameter = 0;
	}
	// echo $jumlahParameter."<br/>";

    // PARSING FUNGSIPARAMETER
	$x = 1;
    $y = 1;
    $rincianFungsiParameter = explode(',', $fungsiParameter);
    $jumlahFungsiParameter = count($rincianFungsiParameter);
    if ($jumlahFungsiParameter > 1) {
	    foreach ($rincianFungsiParameter as $parseFungsiParameter) {
	    	if ($y > 2) {
	    		$y = 1;	
	        	$x++;
	        	// echo "<br/>";
	    	}
	    	$parseFungsiParameter = ltrim($parseFungsiParameter);
	        $arrayFungsiParameter[$x][$y] = $parseFungsiParameter;
	        // echo $arrayFungsiParameter[$x][$y];
	        $y++;
	    }
	    $jumlahFungsiParameter = $x;
	} else{
		$jumlahFungsiParameter = 0;
	}
	// echo $jumlahFungsiParameter."<br/>";

	// MELAKUKAN KONEKSI BARU
    $sql_lihat_host = "SELECT * FROM tb_host WHERE id_host = '$host'";
    $query_lihat_host = mysql_query($sql_lihat_host);
    $lihat_host = mysql_fetch_array($query_lihat_host);
    $koneksiBaru = mysql_connect($lihat_host['host'], $lihat_host['username'], $lihat_host['password']) or die("Connection Error: " . mysql_error());
    mysql_select_db($lihat_host['db'], $koneksiBaru) or die("Error conecting to db.");

	// echo $host." | ".$sql." | ".$keyword." | ".$tabel." | ".$output." | ".$parameter;
    
    

    // =========================== BAGIAN UNTUK MENAMPILKAN HASIL QUERY ===========================
    echo "<div class='contoh'>";
	    echo "<div>Hasil Query : </div>";
	    echo "<div class='font' style='margin-top: 5px;'>";
		switch ($sql) {
			// ----------- Hasil Query Select -----------
			case 'select':
				echo "SELECT ";
				echo "<span class='biru'>";
				if ($jumlahOutput > 1) {
					for ($i=1; $i <= $jumlahOutput; $i++) { 
						if ($arrayOutput[$i][3] == "") {
							echo $arrayOutput[$i][1];
						} else {
							echo $arrayOutput[$i][3]."(".$arrayOutput[$i][1].")";
						}

						if ($i < $jumlahOutput) {
							echo ", ";
						}	
					}
				} else{
					if ($arrayOutput[1][3] == "") {
						echo $arrayOutput[1][1];
					} else {
						echo $arrayOutput[1][3]."(".$arrayOutput[1][1].")";
					}
				}
				echo "</span>";
				echo " FROM ";
				echo "<span class='biru'>";
				if ($jumlahTabel > 1) {
					for ($i=1; $i <= $jumlahTabel; $i++) { 
						echo $arrayTabel[$i];
						if ($i < $jumlahTabel) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayTabel[1];
				}
				echo "</span>";
				echo " WHERE ";
				echo "<span class='biru'>";
				if ($jumlahParameter > 1) {
					for ($i=1; $i <= $jumlahParameter; $i++) { 
						echo $arrayParameter[$i][1];
						echo " ";
						echo $arrayParameter[$i][3];
						echo " '";
						echo $arrayParameter[$i][2];
						echo "'";
						if ($i < $jumlahParameter) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayParameter[1][1];
					echo " ";
					echo $arrayParameter[1][3];
					echo " '";
					echo $arrayParameter[1][2];
					echo "'";
				}
				echo "</span>";
				break;

			// ----------- Hasil Query Insert -----------
			case 'insert':
				echo "INSERT INTO ";
				echo "<span class='biru'>";
				echo $arrayTabel[1];
				echo "</span>";
				echo " (";
				echo "<span class='biru'>";
				if ($jumlahOutput > 1) {
					for ($i=1; $i <= $jumlahOutput; $i++) { 
						echo $arrayOutput[$i][1];
						if ($i < $jumlahOutput) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayOutput[1][1];
				}
				echo "</span>";
				echo ") VALUES (";
				echo "<span class='biru'>";
				if ($jumlahOutput > 1) {
					for ($i=1; $i <= $jumlahOutput; $i++) { 
						echo "'".$arrayOutput[$i][2]."'";
						if ($i < $jumlahOutput) {
							echo ", ";
						}	
					}
				} else{
					echo "'".$arrayOutput[1][2]."'";
				}
				echo "</span>";
				echo ")";
				break;

			// ----------- Hasil Query Update -----------
			case 'update':
				echo "UPDATE ";
				echo "<span class='biru'>";
				echo $arrayTabel[1];
				echo "</span>";
				echo " SET ";
				echo "<span class='biru'>";
				if ($jumlahOutput > 1) {
					for ($i=1; $i <= $jumlahOutput; $i++) { 
						echo $arrayOutput[$i][1];
						echo "='";
						echo $arrayOutput[$i][2];
						echo "'";
						if ($i < $jumlahOutput) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayOutput[1][1];
					echo "='";
					echo $arrayOutput[1][2];
					echo "'";
				}
				echo "</span>";
				echo " WHERE ";
				echo "<span class='biru'>";
				if ($jumlahParameter > 1) {
					for ($i=1; $i <= $jumlahParameter; $i++) { 
						echo $arrayParameter[$i][1];
						echo " ";
						echo $arrayParameter[$i][3];
						echo " '";
						echo $arrayParameter[$i][2];
						echo "'";
						if ($i < $jumlahParameter) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayParameter[1][1];
					echo " ";
					echo $arrayParameter[1][3];
					echo " '";
					echo $arrayParameter[1][2];
					echo "'";
				}
				echo "</span>";
				break;

			// ----------- Hasil Query Delete -----------
			case 'delete':
				echo "DELETE FROM ";
				echo "<span class='biru'>";
				echo $arrayTabel[1];
				echo "</span>";
				echo " WHERE ";
				echo "<span class='biru'>";
				if ($jumlahParameter > 1) {
					for ($i=1; $i <= $jumlahParameter; $i++) { 
						echo $arrayParameter[$i][1];
						echo " ";
						echo $arrayParameter[$i][3];
						echo " '";
						echo $arrayParameter[$i][2];
						echo "'";
						if ($i < $jumlahParameter) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayParameter[1][1];
					echo " ";
					echo $arrayParameter[1][3];
					echo " '";
					echo $arrayParameter[1][2];
					echo "'";
				}
				echo "</span>";
				break;

			// ----------- Hasil Query Function -----------
			case 'function':
				echo "SELECT ";
				echo "<span class='biru'>";
				echo $fungsi;
				echo "</span>";
				echo "(";
				echo "<span class='biru'>";
				if ($jumlahFungsiParameter > 1) {
					for ($i=1; $i <= $jumlahFungsiParameter; $i++) { 
						echo $arrayFungsiParameter[$i][2];
						if ($i < $jumlahFungsiParameter) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayFungsiParameter[$i][2];
				}
				echo "</span>";
				echo ");";
				break;

			// ----------- Hasil Query Procedure -----------
			case 'procedure':
				echo "SELECT ";
				echo "<span class='biru'>";
				echo $fungsi;
				echo "</span>";
				echo "(";
				echo "<span class='biru'>";
				if ($jumlahFungsiParameter > 1) {
					for ($i=1; $i <= $jumlahFungsiParameter; $i++) { 
						echo $arrayFungsiParameter[$i][2];
						if ($i < $jumlahFungsiParameter) {
							echo ", ";
						}	
					}
				} else{
					echo $arrayFungsiParameter[$i][2];
				}
				echo "</span>";
				echo ");";
				break;
		}
		echo "</div>";
	echo "</div>";
	// =========================== AKHIR MENAMPILKAN HASIL QUERY ===========================



	// =========================== BAGIAN UNTUK MENAMPILKAN FORMAT SMS ===========================
	echo "<div class='contoh' style='margin-top: 10px;'>";
	    echo "<div>Format : </div>";
	    echo "<div class='font' style='margin-top: 5px;'>";
	    	echo $keyword;

	    	switch ($sql) {
	    		case 'select':
	    			if ($jumlahParameter == 1) {
	    				echo "<span class='biru'>";
	    					echo $karakter;
	    				echo "</span>";
    					echo $arrayParameter[1][2];
	    			} else if ($jumlahParameter > 1) {
	    				for ($i=1; $i <= $jumlahParameter ; $i++) { 
	    					echo "<span class='biru'>";
	    						echo $karakter;
	    					echo "</span>";
    						echo $arrayParameter[$i][2];
	    				}
	    			}
	    			break;
	    		
	    		case 'insert':
	    			if ($jumlahOutput == 1) {
	    				echo "<span class='biru'>";
	    					echo $karakter;
	    				echo "</span>";
    					echo $arrayOutput[1][2];
	    			} else if ($jumlahOutput > 1) {
	    				for ($i=1; $i <= $jumlahOutput ; $i++) { 
	    					echo "<span class='biru'>";
	    						echo $karakter;
	    					echo "</span>";
    						echo $arrayOutput[$i][2];
	    				}
	    			}
	    			break;

	    		case 'update':
	    			if ($jumlahOutput == 1) {
	    				echo "<span class='biru'>";
	    					echo $karakter;
	    				echo "</span>";
    					echo $arrayOutput[1][2];
	    			} else if ($jumlahOutput > 1) {
	    				for ($i=1; $i <= $jumlahOutput ; $i++) { 
	    					echo "<span class='biru'>";
	    						echo $karakter;
	    					echo "</span>";
    						echo $arrayOutput[$i][2];
	    				}
	    			}

	    			if ($jumlahParameter == 1) {
	    				echo "<span class='biru'>";
	    					echo $karakter;
	    				echo "</span>";
    					echo $arrayParameter[1][2];
	    			} else if ($jumlahParameter > 1) {
	    				for ($i=1; $i <= $jumlahParameter ; $i++) { 
	    					echo "<span class='biru'>";
	    						echo $karakter;
	    					echo "</span>";
    						echo $arrayParameter[$i][2];
	    				}
	    			}
	    			break;

	    		case 'delete':
	    			if ($jumlahParameter == 1) {
	    				echo "<span class='biru'>";
	    					echo $karakter;
	    				echo "</span>";
    					echo $arrayParameter[1][2];
	    			} else if ($jumlahParameter > 1) {
	    				for ($i=1; $i <= $jumlahParameter ; $i++) { 
	    					echo "<span class='biru'>";
	    						echo $karakter;
	    					echo "</span>";
    						echo $arrayParameter[$i][2];
	    				}
	    			}
	    			break;

	    		case 'function':
	    			if ($jumlahFungsiParameter == 1) {
	    				echo "<span class='biru'>";
	    					echo $karakter;
	    				echo "</span>";
    					echo $arrayFungsiParameter[1][2];
	    			} else if ($jumlahFungsiParameter > 1) {
	    				for ($i=1; $i <= $jumlahFungsiParameter ; $i++) { 
	    					echo "<span class='biru'>";
	    						echo $karakter;
	    					echo "</span>";
    						echo $arrayFungsiParameter[$i][2];
	    				}
	    			}
	    			break;

	    		case 'procedure':
	    			if ($jumlahFungsiParameter == 1) {
	    				echo "<span class='biru'>";
	    					echo $karakter;
	    				echo "</span>";
    					echo $arrayFungsiParameter[1][2];
	    			} else if ($jumlahFungsiParameter > 1) {
	    				for ($i=1; $i <= $jumlahFungsiParameter ; $i++) { 
	    					echo "<span class='biru'>";
	    						echo $karakter;
	    					echo "</span>";
    						echo $arrayFungsiParameter[$i][2];
	    				}
	    			}
	    			break;
	    	}

	    echo "</div>";
	echo "</div>";
	// =========================== AKHIR MENAMPILKAN FORMAT SMS ===========================



	// =========================== BAGIAN UNTUK MENYIMPAN DATA KE DATABASE ===========================
	switch ($sql) {
		case 'select':
			if ($jumlahParameter == 1) {
				
			} else if ($jumlahParameter > 1) {
				for ($i=1; $i <= $jumlahParameter ; $i++) { 
					
				}
			}
			break;
		}
	// ===========================    AKHIR MENYIMPAN DATA KE DATABASE     ===========================
?>